package ue.core;

/**
 * A 2-Dimensional Vector class
 *
 * @author ruben.ortlam
 */
public class Vector2d {
	public double x;
	public double y;

	/**
	 * Creates a new Vector and initialize it with the coordinates (0|0).
	 */
	public Vector2d() {
		x = 0;
		y = 0;
	}

	/**
	 * Creates a new Vector and initialize it with given coordinates.
	 *
	 * @param posx
	 *            The x-coordinate of the new vector.
	 * @param posy
	 *            The y-coordinate of the new vector.
	 */
	public Vector2d(double posx, double posy) {
		x = posx;
		y = posy;
	}

	/**
	 * Copies the given Vector.
	 *
	 * @param vec
	 *            The vector to be copied.
	 */
	public Vector2d(Vector2d vec) {
		x = vec.x;
		y = vec.y;
	}
	/**
	 * 
	 */
	public Vector2d(Vector2i v) {
		x = v.x;
		y = v.y;
	}
	
	/**
	 * Returns the distance between this vector and the given vector.
	 *
	 * @param p2
	 *            The second vector.
	 * @return The distance between the two vectors.
	 */
	public double distance(Vector2d p2) {
		return Math.sqrt((y - p2.y) * (y - p2.y) + (x - p2.x) * (x - p2.x));
	}

	/**
	 * Returns the distance between this vector and the given coordinates.
	 *
	 * @param nx
	 *            The x-coordinate of the second vector.
	 * @param ny
	 *            The y-coordinate of the second vector.
	 * @return The distance between the two vectors.
	 */
	public double distance(double nx, double ny) {
		return Math.sqrt((y - ny) * (y - ny) + (x - nx) * (x - nx));
	}

	/**
	 * Sets the x-coordinate of the vector to the given double.
	 *
	 * @param posx
	 *            The new x-coordinate.
	 */
	public void setX(double posx) {
		x = posx;
		return;
	}

	/**
	 * Sets the y-coordinate of the vector to the given double.
	 *
	 * @param posy
	 *            The new y-coordinate.
	 */
	public void setY(double posy) {
		y = posy;
		return;
	}

	/**
	 * Sets the coordinates of the vector to the new coordinates.
	 *
	 * @param posx
	 *            The new x-coordinate.
	 * @param posy
	 *            The new y-coordinate.
	 */
	public void setPos(double posx, double posy) {
		x = posx;
		y = posy;
		return;
	}

	/**
	 * Sets the coordinates of the vector to the coordinates of the second
	 * vector.
	 *
	 * @param nPos
	 *            The vector containing the new coordinates.
	 */
	public void setPos(Vector2d nPos) {
		x = nPos.x;
		y = nPos.y;
		return;
	}

	/**
	 * Adds the coordinates of the second vector to the coordinates of this
	 * vector.
	 *
	 * @param secondVec
	 *            The second vector.
	 */
	public void add(Vector2d secondVec) {
		if (!Double.isNaN(secondVec.x))
			x += secondVec.x;
		if (!Double.isNaN(secondVec.y))
			y += secondVec.y;
		return;
	}

	/**
	 * Adds the given coordinates to the coordinates of this vector.
	 *
	 * @param x
	 *            The x-coordinate to be added to this vector.
	 * @param y
	 *            The y-coordinate to be added to this vector.
	 */
	public void add(double x, double y) {
		this.x += x;
		this.y += y;
	}

	/**
	 * Add the given double(s) to the x-coordinate of this vector.
	 *
	 * @param secXs
	 *            The double(s) to be added to the x-coordinate.
	 */
	public void addX(double... secXs) {
		for (int i = 0; i < secXs.length; i++)
			x += secXs[i];
		return;
	}

	/**
	 * Add the given double(s) to the y-coordinate of this vector.
	 *
	 * @param secYs
	 *            The double(s) to be added to the y-coordinate.
	 */
	public void addY(double... secYs) {
		for (int i = 0; i < secYs.length; i++)
			y += secYs[i];
		return;
	}

	/**
	 * Returns a string containing the coordinates and the type of this vector.
	 *
	 * @return The string summarizing the vector.
	 */
	public String toString() {
		return "Vec2: X: " + x + " Y: " + y;
	}

	/**
	 * Returns a string containing the coordinates as integers and the type of
	 * this vector.
	 *
	 * @return The string summarizing the vector.
	 */
	public String toIntString() {
		return "Vec2: X: " + (int) (x) + " Y: " + (int) (y);
	}

	/**
	 * Returns the length of this vector.
	 *
	 * @return The length of this vector.
	 */
	public double length() {
		return Math.abs(Math.sqrt(x * x + y * y));
	}

	/**
	 * Multiplies the coordinates of this vector with the given multiplier.
	 *
	 * @param multiplier
	 *            The multiplier.
	 */
	public void multiply(double multiplier) {
		x *= multiplier;
		y *= multiplier;
		return;
	}

	/**
	 * Normalizes this vector.
	 */
	public void normalize() {
		double tmp = length();
		x /= tmp;
		y /= tmp;
		return;
	}
}
